<?php
namespace Book\Controller;

use Base\Controller\AdminBaseController;

class BookController extends AdminBaseController {

    protected $bookModel;
	protected $bookCodeModel;
	
	function _initialize() {
		parent::_initialize();

		$this->bookModel = D("Book/Book");
		$this->bookCodeModel = D("Book/BookCode");
	}
	
	/******************************** JSON *********************************/
	/** 查看某信息 */
	public function find() {
		$id = $_GET['id'];
		
		$book = $this->bookModel->where(array("id" => $id))->find();
		if (!$book) {
			$data['code'] = 499;
			$data['msg'] = '未找到目标！';
			$this->ajaxReturn($data);
		}
		
		// bar
		$book['bar'] = $book['province_id'] . "," . $book['address'];
		
	    $data['code'] = 0;
	    $data['msg'] = 'OK';
	    $data['obj'] = $book;
	    $this->ajaxReturn($data);
	}

	/** 查看所有列表 */
	public function dataList() {
		$dataList = $this->bookModel->dataList($_GET);
	    $this->ajaxReturn($dataList);
	}

	/** 根据cid查看所有列表 */
	public function dataListByBookCatId() {
	    $bookCatId = $_GET['cid'];
	     
	    $bookCatList = M("BookCat")->select();
	    $idsList = $this->getIds($bookCatList, $bookCatId, []);
// 	    dump($idsList);
	    
		$_GET['inKey'] = 'cid';
		$_GET['inValue'] = $idsList;
		$_GET['notEqualMap'] = array('status' => -1);
		$dataList = $this->bookModel->dataList($_GET);
		
	    $this->ajaxReturn($dataList);
	}

	/** 修改某信息 */
	public function update() {
		$id = $_GET['id'];
		$bookJson = file_get_contents('php://input');
		$book = json_decode($bookJson, true);

		// update
		$book['url'] = '';											// update reset
		$book['status'] = 0;										// update reset
		$book['update_time'] = date("Y-m-d H:i:s");
		
		// 执行更新
		$result = $this->bookModel->where(array("id" => $id))->save($book);
		if (false === $result) {
			$data['code'] = 499;
			$data['msg'] = '编辑失败！';
			$this->ajaxReturn($data);
		}

		$updatedBook = $this->bookModel->where(array("id" => $id))->find();
			
		$data['code'] = 0;
		$data['msg'] = '编辑成功！';
		$data['obj'] = $updatedBook;
		$this->ajaxReturn($data);
	}

	/** 批量刷新（URL） */
	public function batRefreash() {
		$result = $this->bookModel->select();
		foreach ($result as $v) {
// 		    $v['status'] = 0;
			$v['url'] = C('TASK_RULE') . $v['id'];
			$this->bookModel->where(array("id" => $v['id']))->save($v);
		}
		
		$data['code'] = 0;
		$data['msg'] = '批量刷新完成！';
		$this->ajaxReturn($data);
	}

	/** 批量审核  */
	public function batAudit() {
		$result = $this->bookModel->select();
		foreach ($result as $v) {
		    $v['status'] = 1;
			$this->bookModel->where(array("id" => $v['id']))->save($v);
		}
		
		$data['code'] = 0;
		$data['msg'] = '批量审核完成！';
		$this->ajaxReturn($data);
	}

	/** 审核 */
	public function audit() {
		$id = $_GET['id'];
		$status = $_GET['status'];
		
		$book = $this->bookModel->where(array("id" => $id))->find();
		if (!$book) {
			$data['code'] = 499;
			$data['msg'] = '未找到目标！';
			$this->ajaxReturn($data);
		}
		
		// 更新status & update
		$book['status'] = $status;
		$book['update_time'] = date("Y-m-d H:i:s");
		
		// 执行更新
		$result = $this->bookModel->where(array("id" => $id))->save($book);
		if (false === $result) {
			$data['code'] = 499;
			$data['msg'] = '审核失败！';
			$this->ajaxReturn($data);
		}

		$data['code'] = 0;
		$data['msg'] = '审核完成！';
		$data['obj'] = $book;
		$this->ajaxReturn($data);
	}
	
	/** 排序 */
	public function sortOrder() {
		$result = parent::_sortOrder($this->bookModel);
		if (!$result) {
			$data['code'] = 499;
			$data['msg'] = '编辑排序失败！';
			$this->ajaxReturn($data);
		}

		$data['code'] = 0;
		$data['msg'] = '编辑排序成功！';
		$this->ajaxReturn($data);
	}

	/** 刷新book */
	public function refresh() {
		$id = $_GET['id'];
		$book = $this->bookModel->where(array("id" => $id))->find();
		$codeList = M('BookCode')->where(array("bid" => $id))->select();

		if (count($codeList) > 0) {
			// update code num
			$book['code_count'] = count($codeList);
		} else {
			$book['code_count'] = 0;
		}
        
		// update other
		$book['url'] = '';											// update reset
		$book['status'] = 0;
		$book['update_time'] = date("Y-m-d H:i:s");
		
		// 执行更新
		$result = $this->bookModel->where(array("id" => $id))->save($book);
		if (false === $result) {
			$data['code'] = 499;
			$data['msg'] = '刷新失败！';
			$this->ajaxReturn($data);
		}
        
		$updatedBook = $this->bookModel->where(array("id" => $id))->find();
		
		$data['code'] = 0;
		$data['msg'] = '刷新成功！';
		$data['obj'] = $updatedBook;
		$this->ajaxReturn($data);
	}
	
	/** 删除某信息 */
	public function delete() {
		$id = $_GET['id'];
		
		$book = $this->bookModel->where(array("id" => $id))->find();
		if (!$book) {
			$data['code'] = 499;
			$data['msg'] = '未找到目标！';
			$this->ajaxReturn($data);
		}
		
		$count = $this->categoryModel->where(array("pid" => $id))->count();
		if ($count > 0) {
			$data['code'] = 499;
			$data['msg'] = '该菜单下还有子类，无法删除！';
			$this->ajaxReturn($data);
		}
		
		// 删除BookCode
		M('BookCode')->where(array("bid" => $id))->delete();
		
		if (!$this->bookModel->delete($id)) {
			$data['code'] = 499;
			$data['msg'] = '删除失败！';
			$this->ajaxReturn($data);
		}

		$data['code'] = 0;
		$data['msg'] = '删除成功！';
		$this->ajaxReturn($data);
	}

	/******************************** HTML *********************************/
	/** index */
    public function index() {
		$this->display(':book');
		return;
	}

	/******************************** DO *********************************/
	public function getIds($data, $pid = 0, $idsList = []) {
	    $idsList[] = (int)$pid;
	    foreach ($data as $k => $v) {
	        if ($v['pid'] == $pid) {
	            $idsList = $this->getIds($data, $v['id'], $idsList);
	        }
	    }
	    return $idsList;
	}
	
}